package com.lqq.school.controller;



import com.lqq.school.domain.Student;
import com.lqq.school.mapper.StudentMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Controller
@RequiresRoles("admin")
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @GetMapping("/stu")
    public String studentlist(Model model) {
        List<Student> students = studentMapper.selectList(null);

        model.addAttribute("list", students);
        return "/admin/stulist";
        }

        //添加学生
    @GetMapping("/toAdminAdd")
    public String toAdd() {
        return "/admin/stuadd";
    }

    @PostMapping("add")
    public String add(Student student) {
        //添加用户
        int insert = studentMapper.insert(student);
        if (insert > 0) {
            System.out.println("-------------新增学生成功！");
        }
        return "redirect:/student/stu";
    }



    /*
    @GetMapping("/stu/toUpdate")
    public String stuToUpdate() {
        return "/stu/stuupdate";
    }*/

    @GetMapping("/toUpdate/{stuId}")
    public String toUpdate(Model model, @PathVariable("stuId") Long stuId) {

        Student student = studentMapper.selectById(stuId);
        model.addAttribute("stu", student);
        return "/admin/stuupdate";
    }

    @PostMapping("/update")
    public String update(Student student) {
        int cow = studentMapper.updateById(student);
        if (cow > 0) {
            System.out.println("修改学生成功！");
        }
        return "redirect:/student/stu";
    }

    @GetMapping("/delete/{stuId}")
    public String delete(@PathVariable("stuId") Long stuId) {
        System.out.println("00000000000000000");
        int i = studentMapper.deleteById(stuId);
        if (i > 0) {
            System.out.println("删除用户成功！！");
        }
        return "redirect:/student/stu";
    }
}

